﻿using System.Collections.Generic;

namespace System.Modules.Extensions
{
    /// <summary>
    /// Dictionary Extensions
    /// </summary>
    public static class DictionaryExtensions
    {
        #region TryGetValue
        /// <summary>
        /// Use this method to Try Get Dictionary Value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="values"></param>
        /// <param name="key"></param>
        /// <returns>T</returns>
        public static T TryGetValue<T>(this Dictionary<string, Object> values, string key) where T : new()
        {
            var obj = default(T);
            if (values.ContainsKey(key))
            {
                try
                {
                    obj = (T)values[key];
                }
                catch
                {
                    obj = default(T);
                }
            }
            return obj;
        } 
        #endregion
    }
}
